package com.conseilimmo.bo.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author verpoorten
 */
public final class Convert {

    private static final Logger LOGGER = Logger.getLogger(Convert.class.getName());

    private Convert() {
    }

    /**
     * Permet de convertir sans alourdir le code
     *
     * @param s
     * @return
     */
    public static long convertStringToLong(String s) {
        try {
            if (s != null && s.trim().length() > 0) {
                return Long.parseLong(s);
            } else {
                return 0;
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Problème dans convertStringToLong {0} {1}", new Object[]{e.getCause(), e.getMessage()});
            return 0;

        }
    }

    /**
     * Permet de convertir sans alourdir le code
     *
     * @param sParam
     * @return
     */
    public static Double convertStringToDouble(String sParam) {
        String s = sParam;
        try {
            if (s != null && s.trim().length() > 0) {
                s = s.replace(',', '.');
                return Double.parseDouble(s);
            } else {
                return 0D;
            }
        } catch (NumberFormatException ex) {
            return 0D;
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Problème dans convertStringToDouble {0} {1}", new Object[]{e.getMessage(), e.getCause()});
            return 0D;

        }
    }

    public static String convertDoubleToString(Double d) {
        String s = "";
        if (d != null) {
            s = d.toString();
        }
        return s;

    }

    public static String convertIntToString(Integer i) {
        String s = "";
        Integer ii = i;
        if (ii != null) {
            ii = i;
            s = ii.toString();
        }
        return s;
    }

    public static String convertIntegerToString(Integer ii) {
        String s = "";

        if (ii != null) {
            s = ii.toString();
        }
        return s;
    }

    public static String convertLongToString(Long d) {
        String s = "";
        if (d != null) {
            s = d.toString();
        }
        return s;

    }

    public static String convertCharToString(Character c) {
        if (c != null) {
            return c.toString();
        }
        return null;

    }

    public static String convertDateToString(Date original) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        if (original != null) {
            return dateFormat.format(original);
        } else {
            return null;
        }
    }

    public static Date convertStringToDate(String dParam) {
        if (dParam != null && !dParam.isEmpty()) {
            String d = dParam;
            Date date = null;
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                if (d != null) {
                    d = d.replaceAll("[.]", "/");
                    date = sdf.parse(d);
                }
            } catch (Exception e) {
                LOGGER.log(Level.SEVERE, "Problème lors du convertStringToDate {0}", e.getMessage());
            }
            return date;
        } else {
            return null;
        }
    }

    public static boolean convertStringToBoolean(String externe) {
        if ("1".equals(externe)) {
            return true;
        }
        return false;
    }

    public static Character convertStringToCharacter(String s) {
        if (s != null && !s.isEmpty()) {
            return Character.valueOf(s.toCharArray()[0]);
        }
        return null;

    }

    public static Integer convertStringToInteger(String chaine) {
        Integer i = null;
        if (chaine != null && !chaine.isEmpty()) {
            i = Integer.parseInt(chaine);
        }
        return i;

    }

    public static boolean convertIntegerToBoolean(Integer externe) {
        Integer vrai = 1;
        if (externe != null && externe.equals(vrai)) {
            return true;
        }
        return false;

    }

    public static Integer convertBooleanToInteger(boolean progEtudes) {
        if (progEtudes) {
            return 1;

        } else {
            return 0;
        }

    }

}
